#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <IRremoteESP8266.h>        //8266的IRremote库,是esp8266配套的红外库，无需额外配置
#include <IRrecv.h>                 //接收库，IRremoteESP8266里的，不用下载
#include <IRutils.h>                //这个很重要，一定要include一下(resultToTimingInfo就是这个库的)。也是IRremoteESP8266里的,不用下载
#include <IRsend.h>                 //发射库，还是IRremoteESP8266里的,不用下载

int led1 = D0;
int led2 = D1;
const uint16_t kIrLed = 4;  // 设置kIrLed为GPIO4，D2脚红外发射
int ONE_WIRE_BUS = D3;        //DS18B20连接D3脚
int FAN = D4;                 //风扇连接D4脚
int MQ2 = D6;                 //烟雾报警器D6脚
const int sensorPin = D7;     //门锁舵机连接的D8脚
int pos = 0;                  //角度存储变量
int pulsewidth;               //定义脉宽变量
float val = 0;                //声明临时变量
char led1Status = 0;      //定义led1初始状态为off，熄灭状态
char led2Status = 0;      //定义led2初始状态为off，熄灭状态
char lockStatus = 0;      //定义门锁初始状态为off，关闭状态
char fanStatus = 0;       //定义风扇初始状态为off,关闭状态
char AirSwitch = 0;       //定义空调初始状态为off,关闭状态
int airTemp = 0;          //定义初始化空调温度
char AirWorkStatus = 0;   //定义初始化空调工作状态为制冷
const char* name  = "888";                       //WIFI名称
const char* password  ="123456789";              //WIFI密码
const char* PRODUCTID  = "587241";               //产品ID
const char* DEVICEID   = "1064814621";           //设备ID
const char* DEV_KEY = "dJm2G1MEH6WrsnPMpVQUnj5d=lk="; //API KEY
const char AUTHENTICATION[] = "temperature";     //鉴权信息
const char DATA_TOPIC_NAME[] = "$dp";            //topic,OneNet数据点上传topic
const char* mqtt_server = "183.230.40.39";       //onenet服务器
char dataTemplate[] = "{\"Temp\":%.2f,\"Smoke\":%.2f}";    //信息模板
char led[] = "{\"Led1\":%d,\"Led2\":%d}";                  //信息模板
char fanLock[] = "{\"Fan\":%d,\"Lock\":%d}";               //信息模板
char air[] = "{\"AirSwitch\":%d,\"AirTemp\":%d}";              //空调开关、温度信息模板
char workStatus[] = "{\"AirWorkStatus\":%d}";              //空调工作状态、温度调节模板
char msgJson[75];
char msg_buf[200];
unsigned short json_len = 0;
char comdata[20];
WiFiClient espClient;
PubSubClient client(espClient);
Servo myservo;                          //创建舵机对象
OneWire oneWire(ONE_WIRE_BUS);	 		  	// 声明连接在单总线上的单总线设备
DallasTemperature sensors(&oneWire);		// 声明一个传感器对象

IRsend irsend(kIrLed);                  // 定义红外发送，将kIrLed设置发送信息
uint16_t power_on[300] = {8894, 4586,  554, 1754,  554, 652,  554, 626,  580, 1752,  554, 652,  554, 1752,  554, 650,  554, 652,  554, 1728,  580, 650,  554, 626,  580, 1730,  578, 628,  578, 626,  580, 650,  554, 650,  556, 652,  556, 650,  556, 628,  578, 652,  554, 626,  578, 1728,  580, 626,  578, 652,  554, 628,  578, 650,  556, 626,  580, 628,  576, 1752,  554, 626,  580, 1752,  554, 626,  580, 650,  556, 1752,  554, 626,  580, 20084,  554, 652,  554, 652,  554, 652,  554, 628,  578, 652,  554, 652,  554, 628,  578, 652,  554, 626,  578, 628,  578, 652,  554, 652,  554, 654,  552, 652,  552, 630,  576, 652,  552, 628,  578, 626,  578, 630,  576, 652,  552, 652,  554, 652,  554, 630,  576, 652,  552, 652,  552, 628,  578, 626,  580, 652,  552, 652,  554, 652,  554, 1754,  552, 1754,  552, 40096,  8896, 4582,  554, 1752,  554, 650,  554, 652,  554, 1726,  582, 650,  554, 1754,  554, 652,  554, 650,  554, 1752,  554, 626,  580, 652,  554, 1754,  554, 650,  554, 650,  554, 626,  578, 650,  554, 650,  554, 650,  556, 650,  554, 650,  554, 650,  554, 1728,  580, 650,  554, 624,  582, 624,  580, 650,  556, 650,  554, 652,  554, 1752,  554, 1752,  556, 1752,  554, 652,  554, 626,  578, 1726,  582, 650,  554, 20058,  580, 650,  556, 650,  554, 650,  554, 650,  556, 650,  554, 626,  578, 650,  556, 650,  554, 650,  556, 624,  580, 650,  556, 624,  582, 650,  556, 624,  580, 626,  578, 650,  554, 650,  554, 650,  556, 624,  582, 624,  580, 626,  580, 1724,  582, 650,  554, 650,  554, 626,  580, 650,  556, 652,  556, 650,  556, 626,  578, 1730,  578, 1752,  556, 1726,  580};
uint16_t power_off[300] = {8920, 4564,  574, 1756,  552, 654,  552, 628,  576, 630,  576, 654,  552, 1754,  552, 652,  552, 654,  552, 1730,  578, 630,  576, 626,  578, 1754,  552, 654,  552, 628,  576, 626,  578, 654,  552, 628,  578, 654,  550, 628,  578, 654,  552, 652,  552, 1730,  578, 652,  554, 652,  552, 654,  552, 652,  552, 654,  552, 652,  554, 1754,  552, 652,  552, 1730,  576, 652,  554, 652,  552, 1728,  578, 652,  552, 20088,  552, 654,  552, 654,  552, 652,  552, 630,  576, 652,  552, 628,  578, 654,  552, 652,  552, 654,  552, 654,  552, 652,  552, 654,  552, 628,  578, 654,  552, 654,  552, 628,  578, 654,  552, 628,  578, 654,  552, 628,  578, 628,  576, 652,  554, 652,  552, 654,  552, 654,  552, 628,  578, 626,  578, 654,  552, 628,  576, 654,  552, 1756,  550, 654,  552, 40072,  8920, 4560,  578, 1728,  578, 654,  554, 654,  552, 654,  552, 652,  552, 1754,  554, 654,  552, 628,  578, 1728,  578, 654,  552, 628,  578, 1730,  578, 652,  552, 654,  552, 626,  580, 652,  552, 654,  552, 626,  580, 654,  550, 654,  552, 652,  554, 1754,  554, 628,  576, 654,  552, 652,  552, 654,  552, 628,  578, 652,  552, 1728,  578, 1730,  576, 1728,  580, 652,  554, 652,  552, 1754,  552, 628,  576, 20058,  580, 652,  554, 652,  554, 652,  554, 652,  554, 652,  554, 652,  554, 624,  580, 626,  578, 652,  552, 652,  554, 652,  554, 652,  554, 652,  554, 626,  580, 652,  554, 652,  554, 626,  578, 650,  554, 652,  554, 652,  554, 652,  554, 1754,  554, 652,  554, 628,  578, 652,  554, 652,  556, 650,  554, 626,  580, 652,  554, 1728,  578, 1728,  580, 626,  578};
uint16_t cool[300] = {8896, 4560,  580, 1726,  580, 650,  556, 624,  580, 1726,  582, 652,  554, 1752,  556, 628,  578, 650,  556, 1726,  582, 650,  556, 650,  556, 1752,  556, 650,  554, 650,  556, 624,  580, 652,  554, 650,  556, 624,  582, 626,  580, 650,  556, 650,  556, 1728,  580, 626,  580, 650,  556, 650,  556, 650,  556, 624,  580, 650,  556, 1752,  554, 650,  556, 1726,  582, 650,  556, 626,  578, 1752,  556, 650,  556, 20084,  554, 650,  556, 650,  556, 624,  580, 650,  556, 650,  556, 650,  556, 650,  556, 650,  554, 650,  556, 650,  556, 650,  554, 624,  582, 624,  582, 624,  582, 650,  554, 628,  578, 650,  556, 624,  582, 650,  554, 652,  554, 650,  556, 624,  580, 650,  554, 650,  556, 650,  556, 650,  554, 650,  554, 650,  554, 624,  580, 650,  556, 1726,  582, 1752,  554, 40094,  8894, 4584,  554, 1752,  556, 626,  578, 624,  582, 1752,  556, 624,  580, 1752,  554, 626,  580, 624,  580, 1752,  554, 652,  556, 650,  556, 1726,  580, 650,  554, 626,  580, 624,  582, 624,  580, 650,  556, 650,  554, 650,  554, 624,  582, 650,  554, 1752,  554, 626,  578, 652,  554, 650,  556, 650,  556, 650,  556, 624,  582, 1752,  556, 1726,  582, 1752,  554, 650,  556, 650,  556, 1752,  556, 650,  556, 20082,  556, 650,  554, 650,  556, 624,  582, 650,  556, 624,  582, 650,  554, 650,  554, 626,  580, 650,  554, 650,  554, 624,  582, 650,  556, 650,  554, 650,  556, 650,  556, 650,  554, 650,  556, 650,  554, 652,  554, 626,  580, 626,  580, 1726,  580, 650,  556, 626,  578, 652,  554, 650,  556, 624,  582, 650,  556, 624,  580, 1752,  556, 1752,  556, 1726,  580};
uint16_t warm[300] = {8944, 4582,  556, 624,  580, 626,  580, 1752,  554, 1752,  556, 1752,  556, 1752,  554, 650,  556, 626,  580, 1726,  582, 626,  580, 626,  580, 1724,  582, 650,  554, 626,  580, 628,  576, 650,  556, 650,  556, 650,  556, 624,  580, 650,  556, 650,  554, 1726,  580, 626,  580, 650,  556, 650,  554, 650,  556, 650,  556, 624,  580, 1728,  580, 650,  554, 1752,  556, 628,  578, 650,  556, 1726,  580, 626,  580, 20084,  554, 626,  580, 626,  580, 652,  554, 650,  556, 650,  554, 652,  554, 650,  556, 626,  578, 626,  578, 626,  580, 626,  580, 650,  556, 624,  580, 650,  554, 650,  556, 650,  554, 652,  552, 624,  582, 650,  554, 650,  554, 650,  554, 624,  582, 650,  554, 652,  554, 650,  556, 650,  554, 652,  558, 646,  554, 1752,  556, 1726,  582, 1752,  554, 1754,  554, 40064,  8920, 4584,  554, 650,  554, 650,  556, 1752,  556, 1724,  582, 1752,  554, 1728,  580, 650,  554, 628,  578, 1726,  580, 626,  580, 650,  554, 1728,  580, 650,  554, 626,  580, 652,  554, 652,  554, 650,  556, 626,  580, 650,  556, 650,  554, 652,  554, 1754,  554, 624,  582, 650,  554, 652,  554, 624,  582, 650,  556, 624,  580, 1752,  554, 1752,  556, 1752,  554, 652,  554, 652,  554, 1752,  554, 652,  554, 20086,  554, 650,  554, 652,  554, 650,  554, 626,  580, 650,  554, 628,  578, 624,  580, 626,  580, 650,  554, 650,  554, 652,  554, 652,  554, 652,  554, 650,  556, 650,  554, 624,  580, 650,  556, 650,  554, 650,  554, 650,  556, 624,  582, 650,  554, 1728,  580, 652,  554, 650,  554, 624,  582, 650,  554, 626,  580, 1752,  556, 1728,  578, 626,  580, 650,  554};
uint16_t reduceTemp[300] = {8920, 4584,  556, 650,  554, 652,  554, 1728,  578, 1754,  554, 1724,  582, 1726,  582, 650,  554, 626,  580, 1726,  582, 624,  582, 650,  554, 1754,  554, 650,  554, 652,  552, 650,  554, 624,  582, 624,  580, 652,  554, 650,  554, 650,  554, 624,  582, 1752,  554, 626,  580, 650,  556, 650,  556, 650,  554, 626,  582, 650,  556, 1728,  578, 652,  554, 1752,  554, 624,  580, 650,  556, 1752,  554, 624,  582, 20058,  580, 650,  556, 650,  556, 624,  580, 626,  580, 652,  554, 650,  556, 624,  580, 626,  580, 650,  556, 650,  554, 652,  554, 650,  554, 650,  554, 652,  554, 650,  554, 650,  556, 650,  556, 624,  580, 652,  554, 650,  556, 624,  580, 652,  554, 624,  580, 650,  556, 626,  580, 650,  554, 650,  554, 650,  556, 1752,  554, 1726,  580, 1730,  578, 1726,  582, 40096,  8892, 4558,  582, 624,  582, 650,  554, 1754,  552, 1726,  582, 1752,  554, 1754,  552, 652,  554, 650,  554, 1752,  554, 652,  554, 624,  582, 1726,  582, 650,  556, 624,  580, 652,  554, 652,  554, 652,  554, 652,  554, 624,  582, 650,  556, 626,  580, 1754,  554, 652,  554, 652,  554, 652,  554, 626,  580, 650,  554, 626,  580, 1728,  580, 1726,  582, 1726,  582, 624,  582, 650,  554, 1728,  580, 652,  554, 20062,  580, 652,  554, 650,  554, 650,  554, 624,  582, 652,  554, 650,  554, 652,  554, 652,  554, 650,  556, 652,  554, 626,  578, 624,  582, 626,  580, 626,  580, 650,  554, 652,  556, 650,  554, 626,  580, 652,  554, 652,  554, 624,  580, 624,  582, 1754,  552, 626,  580, 652,  554, 626,  580, 626,  578, 650,  556, 1754,  554, 1752,  554, 650,  554, 624,  582};
uint16_t raiseTemp[300] = {8888, 4572,  570, 638,  566, 638,  568, 1740,  566, 1742,  566, 1742,  566, 1742,  564, 666,  540, 640,  564, 666,  542, 1740,  566, 640,  564, 1740,  566, 642,  564, 640,  566, 666,  540, 640,  566, 666,  540, 640,  566, 640,  566, 640,  566, 666,  538, 1768,  540, 642,  564, 666,  540, 638,  566, 640,  564, 666,  540, 640,  566, 1768,  540, 640,  566, 1742,  566, 638,  566, 640,  566, 1740,  566, 640,  564, 20098,  542, 636,  568, 634,  570, 636,  570, 664,  542, 664,  542, 662,  544, 662,  542, 638,  568, 662,  544, 636,  570, 638,  568, 640,  566, 636,  568, 662,  544, 634,  570, 636,  568, 660,  546, 632,  572, 632,  574, 662,  544, 636,  568, 632,  572, 662,  544, 662,  544, 662,  544, 660,  544, 638,  568, 634,  572, 636,  568, 640,  566, 662,  544, 634,  572, 40106,  8886, 4590,  550, 654,  550, 656,  550, 1758,  550, 1738,  570, 1760,  548, 1760,  548, 656,  550, 658,  548, 634,  570, 1730,  576, 656,  550, 1736,  572, 632,  572, 654,  552, 630,  576, 626,  580, 628,  580, 626,  578, 654,  552, 630,  574, 654,  552, 1730,  576, 654,  550, 626,  580, 656,  550, 656,  548, 628,  578, 626,  580, 1756,  550, 1756,  550, 1730,  578, 628,  578, 654,  552, 1736,  570, 654,  552, 20060,  578, 628,  578, 656,  550, 632,  574, 628,  578, 632,  574, 634,  572, 632,  574, 630,  576, 658,  548, 654,  552, 636,  570, 634,  570, 656,  550, 626,  578, 636,  570, 632,  574, 632,  574, 656,  550, 658,  548, 630,  574, 628,  578, 654,  554, 1758,  548, 626,  578, 628,  578, 654,  552, 636,  570, 636,  570, 656,  550, 634,  572, 1736,  572, 634,  570};

//连接WIFI
void connectWIFI(){
  Serial.begin(115200);                 // 启动串口通讯,波特率设置为115200
    
  Serial.println("未连接");
  Serial.println("开始连接");
  WiFi.begin(name , password);
  Serial.print("正在连接到");
  Serial.print(name );
  
  while (WiFi.status() != WL_CONNECTED) {    //判定网络状态
    delay(500);
    
    Serial.print("网络连接成功"); 
    Serial.print("连接到的接入点名字:");
    Serial.println(name );                  // 告知用户建立的接入点WiFi名
    Serial.print("连接到的接入点密码:");
    Serial.println(password);               // 显示用户建立的接入点WiFi密码
    Serial.print("无线模式成功开启，网络连接成功");
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.print("无线IP地址为: ");
    Serial.println(WiFi.localIP());
  }
    delay(500);
}

//回调函数 接收数据
void callback(char* topic,byte* payload,unsigned int length){
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  switch ((char)payload[0]){
    case '0':
      closeLed1();
    break;
    case '1':
      lightLed1();
    break;
    case '2':
      closeLock();
    break;
    case '3':
      openLock();
    break;
    case '4':
      closeFan();
    break;
    case '5':
      openFan();
    break;
    case '6':
      closeLed2();
    break;
    case '7':
      lightLed2();
    break;
    case 'A':
      closeAir();
    break;
    case 'B':
      openAir();
    break;
    case 'C':
      setwarm();
    break;
    case 'D':
      setcool();
    break;
    case 'E':
      raise();
    break;
    case 'F':
      reduce();
    break;
    default:
      Serial.println("指令错误！");
    break;

  }
}
void lightLed1(){
  pinMode(led1,OUTPUT);    //设置管脚为输出模式
  digitalWrite(led1,1);    //向管脚写入1,设置为高电平,LED1被点亮
  led1Status = 1;
}
void lightLed2(){
  pinMode(led2,OUTPUT);    //设置管脚为输出模式
  digitalWrite(led2,1);    //向管脚写入1,设置为高电平,LED2被点亮
  led2Status = 1;
}
void closeLed1(){
  pinMode(led1,OUTPUT);    //设置管脚为输出模式
  digitalWrite(led1,0);    //向管脚写入0,设置为低电平,LED1被关闭
  led1Status = 0;
}
void closeLed2(){
  pinMode(led2,OUTPUT);    //设置管脚为输出模式
  digitalWrite(led2,0);    //向管脚写入0,设置为低电平,LED2被关闭
  led2Status = 0;
}
void closeLock(){
  pinMode(sensorPin,OUTPUT);      //设置舵机接口为输出接口
  unsigned char i;
  //+90°
  for(int i=0;i<100;i++){
    digitalWrite(sensorPin,HIGH);
    delayMicroseconds(2300);
    digitalWrite(sensorPin,LOW);
    delayMicroseconds(17700);
  }
  lockStatus = 0;
}
void openLock(){
  pinMode(sensorPin,OUTPUT);      //设置舵机接口为输出接口
  unsigned char i;
  //0°
  for(int i=0;i<100;i++){
    digitalWrite(sensorPin,HIGH);
    delayMicroseconds(1500);
    digitalWrite(sensorPin,LOW);
    delayMicroseconds(18500);
  } 
  lockStatus = 1;
}
void closeFan(){
  pinMode(FAN,OUTPUT);
  digitalWrite(FAN,0);
  fanStatus = 0;
}
void openFan(){
  pinMode(FAN,OUTPUT);
  digitalWrite(FAN,1);
  fanStatus = 1;
}
void closeAir(){
  AirSwitch = 0;
  airTemp = 0;
  irsend.sendRaw(power_off,281,38); //以38KHz发送红外数据
}
void openAir(){
  AirSwitch = 1;
  airTemp = 25;
  AirWorkStatus = 0;
  irsend.sendRaw(power_on,281,38); //以38KHz发送红外数据
}
void setwarm(){
  irsend.sendRaw(warm,281,38); //以38KHz发送红外数据
  airTemp = airTemp + 1;
}
void setcool(){
  irsend.sendRaw(cool,281,38); //以38KHz发送红外数据
  airTemp = airTemp - 1;
}
void reduce(){
  AirWorkStatus = 0;
  irsend.sendRaw(reduceTemp,281,38); //以38KHz发送红外数据
}
void raise(){
  AirWorkStatus = 1;
  irsend.sendRaw(raiseTemp,281,38); //以38KHz发送红外数据
}

//上传空调当前工作模式状态
void sendAirWorkStatus(){
  snprintf(msgJson, 40, workStatus,AirWorkStatus);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
  json_len = strlen(msgJson);                    //msgJson的长度
  msg_buf[0] = char(0x03);                       //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
  msg_buf[1] = char(json_len >> 8);              //数据第二位是要发送的数据长度的高八位
  msg_buf[2] = char(json_len & 0xff);            //数据第三位是要发送数据的长度的低八位
  memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
  msg_buf[3 + strlen(msgJson)] = 0;              //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
  Serial.print("public message:");
  Serial.println(msgJson);
  client.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
	delay(500);
}

//上传空调开关、温度状态
void sendAir(){
  snprintf(msgJson, 40, air,AirSwitch,airTemp);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
  json_len = strlen(msgJson);                    //msgJson的长度
  msg_buf[0] = char(0x03);                       //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
  msg_buf[1] = char(json_len >> 8);              //数据第二位是要发送的数据长度的高八位
  msg_buf[2] = char(json_len & 0xff);            //数据第三位是要发送数据的长度的低八位
  memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
  msg_buf[3 + strlen(msgJson)] = 0;              //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
  Serial.print("public message:");
  Serial.println(msgJson);
  client.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
	delay(500);
}

//上传led灯状态
void sendLed(){
  snprintf(msgJson, 40, led,led1Status,led2Status);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
  json_len = strlen(msgJson);                    //msgJson的长度
  msg_buf[0] = char(0x03);                       //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
  msg_buf[1] = char(json_len >> 8);              //数据第二位是要发送的数据长度的高八位
  msg_buf[2] = char(json_len & 0xff);            //数据第三位是要发送数据的长度的低八位
  memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
  msg_buf[3 + strlen(msgJson)] = 0;              //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
  Serial.print("public message:");
  Serial.println(msgJson);
  client.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
	delay(500);
}

//上传风扇、门锁状态
void sendFanLock(){
  snprintf(msgJson, 40, fanLock,fanStatus,lockStatus);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
  json_len = strlen(msgJson);                    //msgJson的长度
  msg_buf[0] = char(0x03);                       //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
  msg_buf[1] = char(json_len >> 8);              //数据第二位是要发送的数据长度的高八位
  msg_buf[2] = char(json_len & 0xff);            //数据第三位是要发送数据的长度的低八位
  memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
  msg_buf[3 + strlen(msgJson)] = 0;              //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
  Serial.print("public message:");
  Serial.println(msgJson);
  client.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
	delay(500);
}

//上传温度、烟雾数据数据到OneNet    应用DS18B20数字温度传感器、MQ2温度传感器
void sendTemp(){
  
  val = analogRead(MQ2);    //读取MQ2口的电压并赋值给val
  sensors.requestTemperatures();
  Serial.print("此时测量的温度为：");
  float tempC = sensors.getTempCByIndex(0);	      // 获取索引号0的传感器摄氏温度数据，并串口输出
	Serial.println(" ℃\n");
  snprintf(msgJson, 40, dataTemplate, tempC,val);//将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
  json_len = strlen(msgJson);                    //msgJson的长度
  msg_buf[0] = char(0x03);                       //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
  msg_buf[1] = char(json_len >> 8);              //数据第二位是要发送的数据长度的高八位
  msg_buf[2] = char(json_len & 0xff);            //数据第三位是要发送数据的长度的低八位
  memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
  msg_buf[3 + strlen(msgJson)] = 0;              //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
  Serial.print("public message:");
  Serial.println(msgJson);
  client.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
	delay(500);
}


void setup()
{    
  pinMode(MQ2,INPUT);
  Serial.begin(115200);
  connectWIFI();
  client.setServer(mqtt_server, 6002);
  client.connect(DEVICEID,PRODUCTID,DEV_KEY);
  client.setCallback(callback);
  irsend.begin();                                    //红外初始化
  Serial.println("Start Red message Recv!");         // 告诉串口监视器，红外接收的工作已经初始化完成。
}
void loop() {
  delay(100); 
  if (!client.connected()) {
    client.setServer(mqtt_server, 6002);
    client.connect(DEVICEID,PRODUCTID,DEV_KEY);
    delay(1000);
  }
  if(!WiFi.isConnected()){
    connectWIFI();
  }
  client.loop();  //客户端循环检测WiFi、OneNet是否连接。
  sendTemp();
  sendLed();
  sendFanLock();
  sendAirWorkStatus();
  sendAir();
}

